<?php

class User_couponController extends BaseController {
	public function listAction() {
		$uid = $this->user_info['uid'];
		$status = intval(Request::getParams('status', -1));
		$page = intval(Request::getParams('page', 1));
		$pageSize = intval(Request::getParams('pageSize', 20));

		$where = $value = [];
		$where[] = 'uid=?';
		$value[] = $uid;
		if ($status != -1) {
			$where[] = 'status=?';
			$value[] = $status;
		}
		Response::ajax(200, 'success', self::model('user_coupon')->getList($where, $value, $page, $pageSize));
	}

	public function getCouponAction() {
		$uid = $this->user_info['uid'];
		$couponid = intval(Request::getParams('couponid'));
		$code = Request::getParams('code');
		if ($couponid) {
			$coupon = self::model('coupon')->getOne('coupon_id=?', $couponid);
			if (!$coupon) {
				Response::ajax(NORMAL_ERROR, '优惠券已过期或者不存在');
			}
		} elseif ($code) {
			$coupon = self::model('coupon')->getOne('code=?', $code);
			if (!$coupon) {
				Response::ajax(NORMAL_ERROR, '优惠券已过期或者不存在');
			}
		} else {
			Response::ajax(NORMAL_ERROR, '优惠券已过期或者不存在');
		}
		$now = date('Y-m-d H:i:s', time());
		if ($coupon['start_time'] > $now || $coupon['end_time'] < $now || $coupon['status'] == 2 || $coupon['total'] < 1) {
			Response::ajax(NORMAL_ERROR, '优惠券已过期或者不存在');
		}

		$usercoupon = $usercoupon = self::model('user_coupon')->getOne(['coupon_id=?', 'uid=?'], [$coupon['coupon_id'], $uid]);
		if ($usercoupon) {
			Response::ajax(NORMAL_ERROR, '您已经领取过该优惠券,不要贪心哦！');
		}
		if ($coupon['time_type'] == 1) {
			$s_time = time();
			$e_time = time() + ($coupon['use_day'] * 86400);
		} else {
			$s_time = strtotime($coupon['start_time']);
			$e_time = $s_time + ($coupon['use_day'] * 86400);
		}
		$resultData = [
			'uid' => intval($uid),
			'coupon_id' => intval($coupon['coupon_id']),
			'coupon_name' => $coupon['name'],
			'rebate' => intval($coupon['rebate']),
			'min_price' => floatval($coupon['min_price']),
			'price' => floatval($coupon['price']),
			'isnew' => intval($coupon['isnew']) == 1 ?: 0,
			'shopid' => intval($coupon['shopid']),
			'type' => intval($coupon['type']),
			'e_time' => intval($e_time),
			's_time' => intval($s_time),

		];
		$data1['total'] = $coupon['total'] - 1;
		$data1['use_total'] = $coupon['use_total'] + 1;
		$flag = self::model('coupon')->update($data1, 'coupon_id=?', $coupon['coupon_id']);
		if (!$flag) {
			Response::ajax(NORMAL_ERROR, '优惠券领取失败！');
		}
		Response::ajax(200, 'success', self::model('user_coupon')->insert($resultData));
	}

	public function couponlistAction() {
		$isnew = intval(Request::getParams('isnew'));
		$uid = $this->user_info['uid'];
		$usercoupon = $usercoupon = self::model('user_coupon')->getOne(['isnew=?', 'uid=?'], [1, $uid]);
		if ($usercoupon) {
			Response::ajax(200, 'success', []);
		}
		if ($isnew) {
			$where[] = 'isnew=?';
			$value[] = $isnew;
			$where[] = 'status=?';
			$value[] = 1;
		}
		$coupon = self::model('coupon')->getAll($where, $value);
		$coupons = [];
		foreach ($coupon as $key => $value) {
			$coupons[$key]['coupon_id'] = intval($value['coupon_id']);
			$coupons[$key]['name'] = $value['name'];
			$coupons[$key]['shopid'] = intval($value['shopid']);
			$coupons[$key]['type'] = intval($value['type']);
			$coupons[$key]['price'] = intval($value['price']);
			$coupons[$key]['min_price'] = intval($value['min_price']);
			$coupons[$key]['time_type'] = intval($value['time_type']);
			$coupons[$key]['time_type'] = intval($value['time_type']);
		}
		Response::ajax(200, 'success', $coupons);
	}
}
